﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Tetris_AntiGravity
{
    class SpriteSimple : Microsoft.Xna.Framework.DrawableGameComponent
    {
        public Texture2D textura;
        public Point frameSize;
        public Point currentFrame = new Point(0, 0);
        public Point sheetSize;
        public Point posicion = new Point(0, 0);

        int timeSinceLastFrame = 0;
        int millisecondsPerFrame = 500;

        public SpriteSimple(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }


        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend);
            spriteBatch.Draw(
                this.textura, 
                Vector2.Zero,
                new Rectangle(currentFrame.X * frameSize.X,
                    currentFrame.Y * frameSize.Y,
                    frameSize.X,
                    frameSize.Y),
                    Color.White, 0, 
                    Vector2.Zero,
                    1, 
                    SpriteEffects.None, 
                    0);
            spriteBatch.End();
            
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
            if (timeSinceLastFrame > millisecondsPerFrame)
            {
                timeSinceLastFrame -= millisecondsPerFrame;
                ++currentFrame.X;
                if (currentFrame.X >= sheetSize.X)
                {
                    currentFrame.X = 0;
                    ++currentFrame.Y;
                    if (currentFrame.Y >= sheetSize.Y)
                        currentFrame.Y = 0;
                }
            }
            
            base.Update(gameTime);
        }



    }
}
